package com.walterlife.javacore;

import java.lang.Thread;
import java.lang.InterruptedException;
import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    Semaphore binary = new Semaphore(1);

    public static void main (String [] args) {
        final SemaphoreTest test = new SemaphoreTest();

        new Thread() {
            @Override
            public void run() {
                test.mutualExclusion();         
            } 
        }.start();

        new Thread() {
            @Override
            public void run() {
                test.mutualExclusion();         
            } 
        }.start();
    }

    private void mutualExclusion() {
        try {

            binary.acquire();

            //mutual exclusive region
            System.out.println(Thread.currentThread().getName() + " inside mutual exclusive region");
            Thread.sleep(1000);

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            binary.release();
            System.out.println(Thread.currentThread().getName() + " outside of mutual exclusive region");
        }
    } 
}
